package com.sbp.message.schedule;

import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dap.utils.DateUtil;
import com.google.common.collect.Sets;
import com.sbp.message.service.impl.ConsumerMessageRecordServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Set;

/**
 * Created by wangmin on 2018/4/28.
 */
public class HistoryConsumerRecordCheckSchedule implements SimpleJob {
    private static final Logger logger = LoggerFactory.getLogger(HistoryConsumerRecordCheckSchedule.class);
    private static final Set<String> tooManyRecordsDestinationSet = Sets.newHashSet("skuChangeMessage");

    @Autowired
    private ConsumerMessageRecordServiceImpl consumerMessageRecordService;


    @Override
    public void execute(ShardingContext shardingContext) {
        // 所有消息保留90天
        final long day90Millis = 90 * 24*3600*1000L;
        Date oldestTime = new Date(System.currentTimeMillis() - day90Millis);
        int deleteRowCount = consumerMessageRecordService.deleteRecordBeforeTime(oldestTime, null);
        logger.info("delete all consumer records before {}, deleteRowCount = {}",
                    DateUtil.format(oldestTime, "yyyyMMddHHmmss"), deleteRowCount);

        // 部分消息记录只保留两天
        final long day2Millis = 2 * 24*3600*1000L;
        oldestTime = new Date(System.currentTimeMillis() - day2Millis);
        deleteRowCount = consumerMessageRecordService.deleteRecordBeforeTime(oldestTime, tooManyRecordsDestinationSet);
        logger.info("delete {} consumer records before {}, deleteRowCount = {}",
                    JSON.toJSONString(tooManyRecordsDestinationSet), DateUtil.format(oldestTime, "yyyyMMddHHmmss"), deleteRowCount);
    }

}
